/*	cupez-dialog v1.0.0 (cross browser, tested with IE6, Safari 5.1, Opera 11, Firefox 28)
	-----------------------------------
	Author	: aKanG cupez
	Created	: 25-04-2014
	website	: http://akangcupez.com
	email	: akangcupez@yahoo.com
	-----------------------------------
*/
var TCtrl = null;
jQuery.fn.showCZDialog = function(options) {
    var thisControl = $(this);
    var thisContent = thisControl.html();

    thisControl.css({
        'position': 'absolute',
        'top': '0',
        'left': '0',
        'width': '100%',
        'height': '100%',
        'z-index': '1000000'
    });

    var setting = $.extend({
        height: '450px',
        width: '600px',
        //margin: '-225px 0 0 -300px',
        closeButton: '',
        mode: 'modal',
        effect: 'none',
        interval: '0'
    }, options);
    
    if (setting.mode == '' || setting.mode == 'modal') {
        var getHeight = setting.height;
        var getWidth = setting.width;
        var closeCtrl = setting.closeButton;

        var intHeight = 0;
        var newHeight = '';
        var checkPX_Height = getHeight.toString().substr(getHeight.length - 2);
        if (!(checkPX_Height.toLowerCase() == 'px')) {
            newHeight = getHeight + 'px';
            intHeight = getHeight;
        } else {
            newHeight = getHeight;
            intHeight = parseInt(getHeight.substring(0, (getHeight.length - 2)));
        }

        var intWidth = 0;
        var newWidth = '';
        var checkPx_Width = getWidth.toString().substr(getWidth.length - 2);
        if (!(checkPx_Width.toLowerCase() == 'px')) {
            newWidth = getWidth + 'px';
            intWidth = getWidth;
        } else {
            newWidth = getWidth;
            intWidth = parseInt(getWidth.substring(0, (getWidth.length - 2)));
        }

        var newMTop = ((intHeight / 2) * -1);
        var newMLeft = ((intWidth / 2) * -1);
        var newMargin = newMTop + "px 0 0 " + newMLeft + "px";

        var addContents = '<div id="divModalBG"></div>' +
	    '<div id="divModalBox">' +
	    '	<div id="divModalCloseBox">' +
	    '		<div id="divModalCloseBtn" title="Close">X</div>' +
	    '	</div>' +
	    '	<div id="divModalWrap">' + thisContent + '</div>' +
	    '	</div></div>';

        //thisControl.html(addContents).show();
        thisControl.html(addContents).show();
        $("#divModalBox").css({
            'height': newHeight,
            'width': newWidth,
            'margin': newMargin
        });

        var newHWrap = (intHeight - (50)) + 'px';
        $("#divModalWrap").css("height", newHWrap);

        if (typeof thisContent == 'undefined' || thisContent == '') {
            $("#divModalWrap").css("border", "0");
        } else {
            if (navigator.userAgent.toLowerCase().indexOf("msie") == -1) {
                if (thisContent.trim() == '') {
                    $("#divModalWrap").css("border", "0");
                } else {
                    $("#divModalWrap").css("border", "1px solid #00D9A3");
                }
            } else {
                $("#divModalWrap").css("border", "1px solid #00D9A3");
            }
        }

        thisControl.on('mouseenter', '#divModalCloseBtn', function() {
            $(this).css("background-color", "#8C0000");
        });

        thisControl.on('mouseleave', '#divModalCloseBtn', function() {
            $(this).css("background-color", "");
        });

        var newCloseButton = (closeCtrl == '') ? '#divModalCloseBtn' : '#divModalCloseBtn, ' + closeCtrl;
        thisControl.on('click', newCloseButton, function() {
            var prevContent = thisControl.html();
            thisControl.empty().html(thisContent).hide();
        });

        $(document).keyup(function(e) {
            var prevContent = thisControl.html();
            if (e.keyCode == 27) {
                thisControl.empty().html(thisContent).hide();
            }
        });
        
    } else if (setting.mode == 'block') {
        var addContentsx = '<div id="divModalBG"></div>';
        thisControl.html(addContentsx).show();
    }
};

jQuery.fn.closeCZDialog = function(options) {
    var setting = $.extend({
        effect: 'none',
        interval: '0'
    }, options);

    var thisControl = $(this);
    var prevContent = $("#divModalWrap").html();

    if (typeof setting.effect == 'undefined' || setting.effect == '' || setting.effect == 'none') {
        thisControl.hide().empty();
    } else if (setting.effect == 'fade') {
    if (typeof setting.interval == 'undefined' || setting.interval == '' ||
            setting.interval == '0' || setting.interval == 0 || isNaN(setting.interval)) {
            thisControl.fadeOut();
        } else {
            thisControl.fadeOut(parseInt(setting.interval));
        }
    }
    thisControl.html(prevContent);
}